var shoucangModel = require('../models/shoucang');
var userModel = require('../models/users');
var shopCarModel = require('../models/shopCar');
var orederModel = require('../models/order');
var adressModel = require('../models/adress');
const presnol = {
    presnol: (req, res, next) => {
        shoucangModel.find({ userId: req.session.loginUser._id }).populate('book_id').then(doc => {
            console.log(doc)
            res.render('presnol', { book: doc, loginUser: req.session.loginUser })
        }).catch(err => {
            console.log(err)
        })
    },
    //获取用户信息
    getUserInfo: (req, res, next) => {
        userModel.findOne({ _id: req.params.id }).then(doc2 => {
            res.json({
                status: 1,
                result: doc2
            })
        }).catch(err => {
            res.json({
                status: 0,
                result: err
            })
        })
    },
    //更新用户信息
    updateUser: (req, res, next) => {
        userModel.updateOne({ _id: req.params.id }, {
            nikename: req.body.userNikename,
            birthday: req.body.userBirthday,
            sex: req.body.userSex,
            aboutMe: req.body.userAboutme
        }).then(doc => {
            userModel.findOne({ _id: req.params.id }).then(doc => {
                res.json({
                    status: 1,
                    result: doc,
                    msg: '个人资料修改成功'
                })
            })
        }).catch(err => {
            res.json({
                status: 0,
                msg: err
            })
        })
    },

    //获取用户订单信息
    getOrder: (req, res, next) => {
        orederModel.find({ user_id: req.session.loginUser._id }).then(doc => {
            res.json({
                status: 1,
                result: doc
            })
        }).catch(err => {
            res.json({
                status: 0,
                result: err
            })
        })
    },

    //查看订单详细信息
    getOrderDetil: (req, res, next) => {
        orederModel.find({ _id: req.body.id, user_id: req.session.loginUser._id }).then(doc => {
            res.json({
                status: 1,
                result: doc
            })
        }).catch(err => {
            res.json({
                status: 0,
                result: err
            })
        })
    },
    //获取地址
    getAdress: (req, res, next) => {
        adressModel.find({ user_id: req.session.loginUser._id }).sort({ sort: 1 }).then(doc => {
            res.json({
                status: 1,
                result: doc
            })
        }).catch(err => {
            console.log(err)
        })
    },

    //增加地址
    addAdress: (req, res, next) => {
        adressModel.create({
            user_id: req.session.loginUser._id,
            who: req.body.adressWho,
            place: req.body.adressPlace,
            detilplace: req.body.aderssPlaceDetil,
            zip_code: req.body.adressZipCode,
            phone: req.body.adressPhone,
            create_at: new Date().toLocaleString(),
            sort: Date.parse(new Date())
        }).then(doc => {
            res.json({
                status: 1,
                msg: "地址增加成功"
            })
        }).catch(err => {
            res.json({
                status: 0,
                msg: err
            })
        })

    },
    //更新地址
    updateAdress: (req, res, next) => {
        adressModel.update({ _id: req.body.id }, {
            who: req.body.adressWho,
            place: req.body.adressPlace,
            detilplace: req.body.aderssPlaceDetil,
            zip_code: req.body.adressZipCode,
            phone: req.body.adressPhone
        }).then(doc => {
            res.json({
                status: 1,
                msg: "地址修改成功"
            })
        }).catch(err => {
            res.json({
                status: 0,
                msg: err
            })
        })
    },

    //删除地址
    deleteAdress: (req, res, next) => {
        adressModel.deleteOne({ _id: req.body.id }).then(doc => {
            res.json({
                status: 1,
                msg: "地址删除成功"
            })
        }).catch(err => {
            res.json({
                status: 0,
                msg: err
            })
        })
    },

    //设置默认地址
    layAdress: (req, res, next) => {
        adressModel.updateMany({ user_id: req.session.loginUser._id }, { is_default: '否', sort: Date.parse(new Date()) }).then(doc => {
            adressModel.update({ _id: req.body.id, user_id: req.session.loginUser._id }, { is_default: '是', sort: 1 }).then(doc => {
                res.json({
                    status: 1,
                    result: doc,
                    msg: '地址设置成功'
                })
            }).catch(err => {
                res.json({
                    status: 0,
                    msg: err
                })
            })
        })
    },

    //验证原密码
    original_password: (req, res, next) => {
        userModel.findOne({ _id: req.session.loginUser._id}).then(doc=>{
            if (doc.password == req.body.original_password){
                res.json({
                    status: 1
                })
            }else{
                res.json({
                    status: 0
                })
            }
        }).catch(err=>{
            res.json({
                status: 0
            })
        })
    },

    //修改密码
    updatePassword: (req, res, next) => {
        userModel.updateOne({ _id: req.session.loginUser._id},{password:req.body.password}).then(doc=>{
            res.json({
                status: 1,
                msg:'修改密码成功'
            })
        }).catch(err => {
            res.json({
                status: 0,
                msg: '修改密码失败'
            })
        })
    }
}
module.exports = presnol;